package com.alstudio.ui.module.userguide;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alstudio.R;
import com.alstudio.ui.base.BaseActivity;
import com.alstudio.view.PageIndicator;
import com.alstudio.view.PagedAdapter;
import com.alstudio.view.PagedView;
import com.alstudio.view.PagedView.OnPagedViewChangeListener;

public class UserGuideActivity extends BaseActivity implements
		OnPagedViewChangeListener {

	private final String TAG = "UserGuideActivity";
	// 此变了用于控制是否用于显示帮助页面
	private boolean isForHelp = false;
	private PagedView mPagedView;
	private PageIndicator mPageIndicator;
	private UseGuideItemAdapter mAdapter;
	private int[] picArray = { R.drawable.help_01, R.drawable.help_02,
			R.drawable.help_03, R.drawable.help_04, R.drawable.help_05,
			R.drawable.help_06 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setupView(this, TAG);
	}

	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setContentView(R.layout.use_guide_layout);
		mPagedView = (PagedView) findViewById(R.id.paged_view);
		mPageIndicator = (PageIndicator) findViewById(R.id.page_indicator);
		mAdapter = new UseGuideItemAdapter(picArray);
		mPagedView.setAdapter(mAdapter);
		mPageIndicator.setDotCount(picArray.length);
		mPagedView.setOnPageChangeListener(this);
	}

	@Override
	protected void setViewClickListener() {
		// TODO Auto-generated method stub
		super.setViewClickListener();
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		isForHelp = getIntent().getBooleanExtra("isForHelp", false);
	}

	private void setActivePage(int page) {

		if (!isForHelp) {
			mPageIndicator.setActiveDot(page);
		} else {
			mPageIndicator.setActiveDot(page);
		}
	}

	private class UseGuideItemAdapter extends PagedAdapter {

		private int[] picArray;

		public UseGuideItemAdapter(int[] picArray) {
			this.picArray = picArray;
		}

		@Override
		public int getCount() {
			return picArray.length;
		}

		@Override
		public Object getItem(int position) {
			return picArray[position];
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView mImage = null;
			if (convertView != null) {
				mImage = (ImageView) convertView;
			} else {
				mImage = new ImageView(UserGuideActivity.this);
			}
			mImage.setBackgroundResource(picArray[position]);

			return mImage;
		}

	}

	@Override
	public void onPageChanged(PagedView pagedView, int previousPage, int newPage) {
		// TODO Auto-generated method stub
		setActivePage(newPage);
	}

	@Override
	public void onStartTracking(PagedView pagedView) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStopTracking(PagedView pagedView) {
		// TODO Auto-generated method stub

	}
}
